//
//  NSDate+ZH_Helper.m
//  ZH_Helper
//
//  Created by caixiang on 2017/10/14.
//  Copyright © 2017年 蔡翔. All rights reserved.
//

#import "NSDate+ZH_Helper.h"

@implementation NSDate (ZH_Helper)

//  时间戳转化为是刚刚、几分钟前、几小时前、几天前等(与当前时间相比)
+ (NSString *)currentDateWithRerence:(NSTimeInterval)timeStamp {
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeStamp];
    return [[NSDate date] prettyDateWithReference:confromTimesp];
}

//  时间转化为是刚刚、几分钟前、几小时前、几天前等(自定义时间相比)
- (NSString *)prettyDateWithReference:(NSDate *)reference {
    NSString *suffix = @"前";
    float different = [reference timeIntervalSinceDate:self];
    if (different < 0) {
        different = -different;
        suffix = @"前";
    }
    // days = different / (24  60  60), take the floor value
    float dayDifferent = floor(different / 86400);
    
    int days   = (int)dayDifferent;
    int weeks  = (int)ceil(dayDifferent / 7);
    int months = (int)ceil(dayDifferent / 30);
    int years  = (int)ceil(dayDifferent / 365);
    
    // It belongs to today
    if (dayDifferent <= 0) {
        // lower than 60 seconds
        if (different < 60) {
            return @"刚刚";
        }
        // lower than 120 seconds => one minute and lower than 60 seconds
        if (different < 120) {
            return [NSString stringWithFormat:@"1分钟%@", suffix];
        }
        // lower than 60 minutes
        if (different < 3600) {
            return [NSString stringWithFormat:@"%d分钟%@", (int)floor(different / 60), suffix];
        }
        // lower than 60  2 minutes => one hour and lower than 60 minutes
        if (different < 7200) {
            return [NSString stringWithFormat:@"1小时%@", suffix];
        }
        // lower than one day
        if (different < 86400) {
            return [NSString stringWithFormat:@"%d小时%@", (int)floor(different / 3600), suffix];
        }
    }
    // lower than one week
    else if (days < 7) {
        return [NSString stringWithFormat:@"%d天%@%@", days, days == 1 ? @"" : @"", suffix];
    }
    // lager than one week but lower than a month
    else if (weeks < 4) {
        return [NSString stringWithFormat:@"%d周%@%@", weeks, weeks == 1 ? @"" : @"", suffix];
    }
    // lager than a month and lower than a year
    else if (months < 12) {
        return [NSString stringWithFormat:@"%d月%@%@", months, months == 1 ? @"" : @"", suffix];
    }
    // lager than a year
    else {
        return [NSString stringWithFormat:@"%d年%@%@", years, years == 1 ? @"" : @"", suffix];
    }
    
    return self.description;
}

//  时间戳转化为当前时间
+ (NSString *)timeIntervalToNSDate:(NSTimeInterval)time {
    //因为时差问题要加8小时 == 28800 sec
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    return currentDateStr;
}
@end
